<template>
  <div class='container'>
    <el-card class="box-card">
<el-form  :model="Addform" ref="AddformRef" label-width="80px">
  <el-row>
    <el-col :span="4">
  <el-form-item label="关键字">
    <el-input v-model="Addquestions.keyword" clearable></el-input>
  </el-form-item>
    </el-col>
    <el-col :span="8">
       <el-form-item >
         <el-button @click="removeClick" class="remove">清空</el-button>
             <el-button type="primary" class="serach" @click="getAddquestions" >搜索</el-button>
  </el-form-item>
    </el-col>
  </el-row>
</el-form>
     <!-- 消息提示的文案 -->
       <el-alert
    :title="`共${total}条数据`"
    type="info"
    show-icon>
  </el-alert>
    <!-- 用户列表展示 -->
      <el-table :data="items" border stripe>
        <el-table-column label="序号" prop="id"></el-table-column>
        <el-table-column label="题型">
          <template slot-scope="scope">
            <span v-if="scope.row.questionType === '1'">单选</span>
            <span v-else-if="scope.row.questionType === '2'">多选</span>
            <span v-else>简答</span>
          </template>
        </el-table-column>
        <el-table-column label="题目编号" prop="questionIDs">
          <template slot-scope="scope">
            <el-button style="color: skyblue" @click="showQueryDialog(item.id)" type="text" v-for="(item, i) in scope.row.questionIDs" :key="i">{{item.number}}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" prop="addTime"></el-table-column>
        <el-table-column label="答题时间(s)" prop="totalSeconds"></el-table-column>
        <el-table-column label="正确率" prop="accuracyRate"></el-table-column>
        <el-table-column label="录入人" prop="userName"></el-table-column>
        <el-table-column label="操作"  >
           <template slot-scope="scope">
             <!-- 删除按钮 -->
             <el-button type="danger" icon="el-icon-delete" circle @click="removeClicke(scope.row.id)" ></el-button>
            </template>
        </el-table-column>
      </el-table>
           <!-- 分页 -->
       <el-pagination
       background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="Addquestions.page"
      :page-sizes="[3, 5, 10, 15]"
      :page-size="Addquestions.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
</el-card>
<!-- 预览按钮对话框 -->
    <el-dialog title="题目预览" :visible.sync="queryDialogVisible" width="50%">
      <questions-preview :queryForm="queryForm" v-if="queryDialogVisible" :options="options" @Closebtn="queryDialogVisible = $event"></questions-preview>
    </el-dialog>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
import questionsPreview from '../components/questions-preview'
export default {
  components: {
    questionsPreview
  },
  data () {
    return {
      Addquestions: {
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      items: [],
      total: 0,
      queryDialogVisible: false,
      options: [],
      id: '',
      Addform: {
        id: '',
        addTime: '',
        userName: '',
        questionIDs: [],
        progressOfAnswer: '',
        accuracyRate: '',
        totalSeconds: '',
        questionType: ''
      },
      queryForm: [],
      myid: null
    }
  },
  created () {
    this.getAddquestions()
  },
  methods: {
    // 获取数据
    async getAddquestions () {
      try {
        const { data } = await randoms(this.Addquestions)
        // console.log(data)
        this.items = data.items
        this.total = data.counts
        this.$message.success('获取组题列表成功！')
      } catch (err) {
        this.$message.error('获取组题列表失败！')
      }
    },
    // 删除
    async removeClicke (id) {
      const confirmResult = await this.$confirm('已成功删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消！')
      }
      try {
        await removeRandoms({
          id: id
        })
        this.getAddquestions()
        this.$message.success('删除成功！')
      } catch (err) {
        this.$message.error('删除失败！')
      }
    },
    // 清空
    removeClick () {
      this.Addquestions.keyword = ''
    },
    // 监听pagesize改变的事件
    handleCurrentChange (newPage) {
      this.Addquestions.page = newPage
      this.getAddquestions()
    },
    // 监听页码值改变的事件
    handleSizeChange (newSize) {
      this.Addquestions.pagesize = newSize
      this.getAddquestions()
    },
    // 点击题目编号预览
    async showQueryDialog (id) {
      console.log(id)
      try {
        const { data } = await detail({ id: id })
        this.queryForm = data
        this.options = data.options
        this.queryDialogVisible = true
      } catch (err) {
        console.log('请求失败', err)
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.el-alert {
  margin: 15px 0;
}
.el-pagination {
margin: 15px 0;
float: right;
}

</style>
